// import 'package:enjoy_life/pages/home/index.dart';
import 'package:enjoy_life/pages/house/building_list.dart';
import 'package:enjoy_life/pages/house/house_detail.dart';
import 'package:enjoy_life/pages/house/house_form.dart';
import 'package:enjoy_life/pages/house/house_list.dart';
import 'package:enjoy_life/pages/house/location_list.dart';
import 'package:enjoy_life/pages/house/room_list.dart';
import 'package:enjoy_life/pages/index.dart';
import 'package:enjoy_life/pages/login/index.dart';
import 'package:enjoy_life/pages/notice/index.dart';
import 'package:enjoy_life/pages/profile/index.dart';
import 'package:enjoy_life/utils/CheckAuth.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    routes: {
      '/': (context) => const NavTabBar(),
      '/detail': (context) => const NoticeDetail(),
      '/login': (context) => const LoginPage(),
      '/homeList': (context) => HouseList(),
    },
    onGenerateRoute: (settings) {
      // 获取 token
      final token = CheckAuth().getToken() ?? '';
      if (token.isEmpty && settings.name != '/login') {
        // 如果页面没有 token 而且去的页面不是登录页
        // 跳转到登录页
        return MaterialPageRoute(builder: (context) => const LoginPage());
      }

      if (settings.name == '/profile') {
        // print(settings.arguments);
        // 如果是首页
        return MaterialPageRoute(
            builder: (context) => ProfilePage(
                  userInfo: settings.arguments as Map,
                ));
      }

      if (settings.name == '/homeList') {
        // 如果页面没有 token 而且去的页面不是登录页
        // 跳转到登录页
        return MaterialPageRoute(builder: (context) => const HouseList());
      }

      if (settings.name == '/locationList') {
        // 如果页面没有 token 而且去的页面不是登录页
        // 跳转到登录页
        return MaterialPageRoute(builder: (context) => const LocationList());
      }

      if (settings.name == '/buildingList') {
        // 如果页面没有 token 而且去的页面不是登录页
        // 跳转到登录页
        return MaterialPageRoute(
            builder: (context) => BuildingList(
                  point: (settings.arguments as Map)['point'],
                ));
      }
      if (settings.name == '/roomList') {
        // 如果页面没有 token 而且去的页面不是登录页
        // 跳转到登录页
        return MaterialPageRoute(
            builder: (context) => RoomList(
                  point: (settings.arguments as Map)['point'],
                  building: (settings.arguments as Map)['building'],
                ));
      }

      if (settings.name == '/houseFrom') {
        // 如果页面没有 token 而且去的页面不是登录页
        // 跳转到登录页
        return MaterialPageRoute(
            builder: (context) => HouseForm(
                  houseInfo: (settings.arguments as Map)['houseInfo'],
                ));
      }

      if (settings.name == '/houseDetail') {
        // 如果页面没有 token 而且去的页面不是登录页
        // 跳转到登录页
        return MaterialPageRoute(
            builder: (context) => HouseDetail(
                  id: (settings.arguments as Map)['id'],
                ));
      }

      // 404 空页面
      return null;
    },
    initialRoute: '/',
  ));
}
